package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.LoginForm;
import org.apache.struts.action.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import dao.TUserDao;

public class LoginAction extends Action {
    public ActionForward execute(ActionMapping map, ActionForm frm,
            HttpServletRequest request, HttpServletResponse response) {

        LoginForm objFrm = (LoginForm) frm;
        String userId = objFrm.getUserId();
        String password = objFrm.getPassword();

        HttpSession ses = request.getSession();
        ses.setAttribute("userId", userId);

        boolean result = true;
        try {
            // TODO 例外で処理しようかどうしようか考え中
            result = TUserDao.isUser(userId, password);
        } catch (Exception e) {
            result = false;
        }

        if (result == true) {
            return map.findForward("success");
        } else {
            ActionMessages errors = new ActionMessages();
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("errors.login"));
            saveErrors(request, errors);
            return map.findForward("error");
        }
    }
}
